<?php

// @formatter:off
/**
 * @file WCComicImage.php
 * @author Alejandro Dario Simi
 * @date $Date: 2014-06-03 03:40:32 +0000 (Tue, 03 Jun 2014) $
 *
 * $Id: WCComicImage.php 112 2014-06-03 03:40:32Z daemonraco@gmail.com $
 * $URL: http://wcomix.googlecode.com/svn/trunk/includes/representations/WCComicImage.php $
 */
// @formatter:on

/**
 * @class WCComicImage @todo doc
 */
class WCComicImage extends WCRepresentation implements WCPreviews {
	/**
	 * @var string[] Check WCRepresentation class documentation.
	 */
	protected $_PropertiesReadOnly = array(
		"id",
		"group",
		"comic",
		"position"
	);
	/**
	 * @var string[] Check WCRepresentation class documentation.
	 */
	protected $_PropertiesBoolean = array(
		"generated",
		"lost"
	);
	/**
	 * @var string Check WCRepresentation class documentation.
	 */
	protected $_PropertiesPrefix = "cim_";
	/**
	 * @var string Check WCRepresentation class documentation.
	 */
	protected $_PropertiesTable = "comic_images";
	/**
	 * @var string Check WCRepresentation class documentation.
	 */
	protected $_PropertiesNameColumn = "path";
	/**
	 * @var WCComic
	 */
	protected $_comic = false;
	/**
	 * @var WCGroup
	 */
	protected $_group = false;
	/**
	 * @var boolean
	 */
	protected $_pathsLoaded = false;
	/**
	 * @var string
	 */
	protected $_previewPath = false;
	/**
	 * @var string
	 */
	protected $_previewUri = false;
	/**
	 * @var string
	 */
	protected $_publicPath = false;
	/**
	 * @var string
	 */
	protected $_publicUri = false;
	//
	// Magic methods.
	public function __toString() {
		return $this->ok() ? "{$this->name()} ({$this->id()}[g:{$this->group},c:{$this->comic},p:{$this->position}])" : "broken item";
	}
	//
	// Public methods.
	/**
	 * 
	 * @return WCComic
	 */
	public function comic() {
		if(!$this->_comic) {
			$this->_comic = WCComicsHolder::I()->item($this->comic);
			if(!$this->_comic || $this->_comic->lost) {
				$this->lost = true;
				$this->_ok = false;
			}
		}
		return $this->_comic;
	}
	public function expand() {
		return $this->comic()->expand();
	}
	public function generatePreviews() {
		//
		// Autoenforcing some checks @{
		$this->comic();
		$this->group();
		// @}
		//
		// Setting a default value to be returned.
		$out = false;
		//
		// Checking that there no issues with this image and that it's
		// preview is not yet generated.
		if($this->ok() && !$this->hasValidPreviews()) {
			$this->expand();

			$imgData = @getimagesize($this->publicPath());
			if($imgData !== false) {
				$imgData = array_merge($imgData, pathinfo($this->publicPath()));
				$imgData["width"] = $imgData[0];
				$imgData["height"] = $imgData[1];
				//
				// Creating a hash code.
				$code = md5($this->publicPath());
				//
				// Setting a URI for thumbnails directory.
				$md5Path = "/{$code[0]}/{$code[1]}{$code[2]}{$code[3]}/{$code}";

				$previewPath = WCSanitizer::WComixPath("/{$this->group}{$md5Path}.{$imgData["extension"]}");
				$out = WCPreviewsManager::I()->createPreview($this->group(), WCPreviewsManager::TypeComicImage, $this->id, $this->position, $previewPath, $this->publicPath());
				//
				// Checking that it is possible to read the file.
				if($out && is_readable($this->publicPath())) {
					//
					// Updating image's MD5 hash.
					$this->md5 = md5(file_get_contents($this->publicPath()));
					//
					// Updating image's SHA-1 hash.
					$this->sha1 = sha1_file($this->publicPath());
					//
					// Updating sizes.
					$this->width = $imgData["width"];
					$this->height = $imgData["height"];
					$this->size = filesize($this->publicPath());

					$this->setValidPreviews(true);
					$this->comic()->setValidPreviews(false);

					$out = $this->hasValidPreviews();
				} else {
					$out = false;
				}
				//
				// Setting to false the flag that controls preview paths.
				$this->_pathsLoaded = false;
				//
				// Loading preview paths.
				$this->loadPaths();
			} else {
				$out = false;
			}
		} elseif($this->hasValidPreviews()) {
			$out = true;
		}
		//
		// Returning result.
		return $out;
	}
	public function group() {
		if(!$this->_group) {
			$this->_group = WCGroupsHolder::I()->item($this->group);
			if(!$this->_group || !$this->_group->ok()) {
				$this->_ok = false;
			}
		}
		return $this->_group;
	}
	public function hasPreview() {
		//
		// Loading paths.
		$this->loadPaths();
		//
		// Returns true when the preview image exists.
		return $this->_previewPath != false;
	}
	public function hasPreviews() {
		return false;
	}
	public function hasValidPreviews() {
		return $this->generated;
	}
	public function isExpanded() {
		return $this->comic()->isExpanded();
	}
	public function path() {
		return $this->publicPath();
	}
	public function publicPath() {
		//
		// Loading paths.
		$this->loadPaths();
		//
		// Returning path.
		return $this->_publicPath;
	}
	public function publicUri() {
		//
		// Loading paths.
		$this->loadPaths();
		//
		// Returning path.
		return $this->_publicUri;
	}
	public function preview() {
		global $wcDefaults;
		return $this->hasPreview() ? $this->_previewUri : $wcDefaults["preview"];
	}
	public function previews() {
		return array();
	}
	public function setValidPreviews($valid = true) {
		if(!$valid) {
			$this->comic()->setValidPreviews($valid);
		}

		$this->generated = $valid;
	}
	public function shrink() {
		return $this->comic()->shrink();
	}
	//
	// Protected methods.
	protected function loadPaths() {
		//
		// Autoenforcing some checks @{
		$this->comic();
		$this->group();
		// @}
		//
		// Checking that there are no issues with this object and that
		// this operation was not run before.
		if($this->ok() && !$this->_pathsLoaded) {
			//
			// Loading global path settings.
			global $wcPaths;

			$previews = WCPreviewsManager::I()->getPreviews($this->group(), WCPreviewsManager::TypeComicImage, $this->id);

			if(count($previews) > 0 && isset($previews[$this->position])) {
				$this->_previewPath = WCSanitizer::SystemPath("{$wcPaths["cache-directory"]}/{$wcPaths["cache-thumbs-dirname"]}/{$previews[$this->position]}");
				$this->_previewUri = WCSanitizer::WComixPath("{$wcPaths["cache-directory-uri"]}/{$wcPaths["cache-thumbs-dirname"]}/{$previews[$this->position]}");
			} else {
				$this->_previewPath = false;
				$this->_previewUri = false;
				$this->setValidPreviews(false);
			}
			//
			// Setting full path and URI.
			$this->_publicPath = "{$this->comic()->expandedPath()}{$this->path}";
			$this->_publicUri = "{$this->comic()->expandedUri()}{$this->path}";
			//
			// Setting the flag to "already done".
			$this->_pathsLoaded = true;
		}
	}
	//
	// Public class methods.
	public static function PositionSort(WCComicImage &$a, WCComicImage &$b) {
		return $a->position > $b->position;
	}
	public static function Sort(WCComicImage &$a, WCComicImage &$b) {
		return self::PositionSort($a, $b);
	}
}
